home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / shell / dialog-0.000 / dialog-0 / dialog-0.6c / checklist.c next >
Encoding:
C/C++ Source or Header  |  1995-08-17  |  12.1 KB  |  398 lines

  1. /*
  2.  *  checklist.c -- implements the checklist box
  3.  *
  4.  *  AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
  5.  *     Stuart Herbert - S.Herbert@sheffield.ac.uk: radiolist extension
  6.  *     Alessandro Rubini - rubini@ipvvis.unipv.it: merged the two
  7.  *
  8.  *  This program is free software; you can redistribute it and/or
  9.  *  modify it under the terms of the GNU General Public License
  10.  *  as published by the Free Software Foundation; either version 2
  11.  *  of the License, or (at your option) any later version.
  12.  *
  13.  *  This program is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with this program; if not, write to the Free Software
  20.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23. #include "dialog.h"
  24.  
  25. static int list_width, check_x, item_x, checkflag;
  26.  
  27. /*
  28.  * Print list item
  29.  */
  30. static void
  31. print_item (WINDOW * win, const char *tag, const char *item, int status,
  32.         int choice, int selected)
  33. {
  34.     int i;
  35.  
  36.     /* Clear 'residue' of last item */
  37.     wattrset (win, menubox_attr);
  38.     wmove (win, choice, 0);
  39.     for (i = 0; i < list_width; i++)
  40.     waddch (win, ' ');
  41.     wmove (win, choice, check_x);
  42.     wattrset (win, selected ? check_selected_attr : check_attr);
  43.     if (checkflag == FLAG_CHECK)
  44.     wprintw (win, "[%c]", status ? 'X' : ' ');
  45.     else
  46.     wprintw (win, "(%c)", status ? 'X' : ' ');
  47.     wattrset (win, menubox_attr);
  48.     waddch (win, ' ');
  49.     wattrset (win, selected ? tag_key_selected_attr : tag_key_attr);
  50.     waddch (win, tag[0]);
  51.     wattrset (win, selected ? tag_selected_attr : tag_attr);
  52.     waddstr (win, tag + 1);
  53.     wmove (win, choice, item_x);
  54.     wattrset (win, selected ? item_selected_attr : item_attr);
  55.     waddstr (win, item);
  56. }
  57.  
  58. /*
  59.  * Display a dialog box with a list of options that can be turned on or off
  60.  * The `flag' parameter is used to select between radiolist and checklist.
  61.  */
  62. int
  63. dialog_checklist (const char *title, const char *prompt, int height, int width,
  64.     int list_height, int item_no, const char * const * items, int flag,
  65.     int separate_output)
  66. {
  67.     int i, x, y, cur_x, cur_y, box_x, box_y;
  68.     int key = 0, button = 0, choice = 0, scroll = 0, max_choice, *status;
  69.     WINDOW *dialog, *list;
  70.  
  71.     checkflag = flag;
  72.  
  73.     /* Allocate space for storing item on/off status */
  74.     if ((status = malloc (sizeof (int) * item_no)) == NULL) {
  75.     endwin ();
  76.     fprintf (stderr,
  77.          "\nCan't allocate memory in dialog_checklist().\n");
  78.     exit (-1);
  79.     }
  80.     /* Initializes status */
  81.     for (i = 0; i < item_no; i++)
  82.     status[i] = !strcasecmp (items[i * 3 + 2], "on");
  83.  
  84.     max_choice = MIN (list_height, item_no);
  85.  
  86.     /* center dialog box on screen */
  87.     x = (COLS - width) / 2;
  88.     y = (LINES - height) / 2;
  89.  
  90. #ifdef HAVE_NCURSES
  91.     if (use_shadow)
  92.     draw_shadow (stdscr, y, x, height, width);
  93. #endif
  94.     dialog = newwin (height, width, y, x);
  95.     mouse_setbase (x, y);
  96.     keypad (dialog, TRUE);
  97.  
  98.     draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
  99.     wattrset (dialog, border_attr);
  100.     wmove (dialog, height - 3, 0);
  101.     waddch (dialog, ACS_LTEE);
  102.     for (i = 0; i < width - 2; i++)
  103.     waddch (dialog, ACS_HLINE);
  104.     wattrset (dialog, dialog_attr);
  105.     waddch (dialog, ACS_RTEE);
  106.     wmove (dialog, height - 2, 1);
  107.     for (i = 0; i < width - 2; i++)
  108.     waddch (dialog, ' ');
  109.  
  110.     if (title != NULL) {
  111.     wattrset (dialog, title_attr);
  112.     wmove (dialog, 0, (width - strlen (title)) / 2 - 1);
  113.     waddch (dialog, ' ');
  114.     waddstr (dialog, title);
  115.     waddch (dialog, ' ');
  116.     }
  117.     wattrset (dialog, dialog_attr);
  118.     print_autowrap (dialog, prompt, width - 2, 1, 3);
  119.  
  120.     list_width = width - 6;
  121.     getyx (dialog, cur_y, cur_x);
  122.     box_y = cur_y + 1;
  123.     box_x = (width - list_width) / 2 - 1;
  124.  
  125.     /* create new window for the list */
  126.     list = subwin (dialog, list_height, list_width,
  127.            y + box_y + 1, x + box_x + 1);
  128.     keypad (list, TRUE);
  129.  
  130.     /* draw a box around the list items */
  131.     draw_box (dialog, box_y, box_x, list_height + 2, list_width + 2,
  132.           menubox_border_attr, menubox_attr);
  133.  
  134.     check_x = 0;
  135.     item_x = 0;
  136.     /* Find length of longest item in order to center checklist */
  137.     for (i = 0; i < item_no; i++) {
  138.     check_x = MAX (check_x, strlen (items[i * 3])
  139.                + strlen (items[i * 3 + 1]) + 6);
  140.     item_x = MAX (item_x, strlen (items[i * 3]));
  141.     }
  142.     check_x = (list_width - check_x) / 2;
  143.     item_x = check_x + item_x + 6;
  144.  
  145.     /* Print the list */
  146.     for (i = 0; i < max_choice; i++)
  147.     print_item (list, items[i * 3], items[i * 3 + 1],
  148.             status[i], i, i == choice);
  149.     wnoutrefresh (list);
  150.  
  151.     /* register the new window, along with its borders */
  152.     mouse_mkbigregion (box_y, box_x, list_height + 2, list_width + 2,
  153.              item_no, item_x /* the threshold */ , 0 /* normal */ );
  154.  
  155.     if (list_height < item_no) {
  156.     wattrset (dialog, darrow_attr);
  157.     wmove (dialog, box_y + list_height + 1, box_x + check_x + 5);
  158.     waddch (dialog, ACS_DARROW);
  159.     wmove (dialog, box_y + list_height + 1, box_x + check_x + 6);
  160.     waddstr (dialog, "(+)");
  161.     }
  162.     x = width / 2 - 11;
  163.     y = height - 2;
  164.     print_button (dialog, "Cancel", y, x + 14, FALSE);
  165.     print_button (dialog, "  OK  ", y, x, TRUE);
  166.     wrefresh (dialog);
  167.  
  168.     while (key != ESC) {
  169.     key = mouse_wgetch (dialog);
  170.     /* Check if key pressed matches first character of
  171.        any item tag in list */
  172.     for (i = 0; i < max_choice; i++)
  173.         if (toupper (key) ==
  174.         toupper (items[(scroll + i) * 3][0]))
  175.         break;
  176.  
  177.     if (i < max_choice ||
  178.         (key >= '1' && key <= MIN ('9', '0' + max_choice)) ||
  179.         key == KEY_UP || key == KEY_DOWN || key == ' ' ||
  180.         key == '+' || key == '-' ||
  181.         (key >= M_EVENT && key - M_EVENT < ' ')) {
  182.         if (key >= '1' && key <= MIN ('9', '0' + max_choice))
  183.         i = key - '1';
  184.         else if (key == KEY_UP || key == '-') {
  185.         if (!choice) {
  186.             if (!scroll)
  187.             continue;
  188.             /* Scroll list down */
  189.             getyx (dialog, cur_y, cur_x);
  190.             if (list_height > 1) {
  191.             /* De-highlight current first item */
  192.             print_item (list, items[scroll * 3],
  193.                     items[scroll * 3 + 1], status[scroll],
  194.                     0, FALSE);
  195.             scrollok (list, TRUE);
  196.             wscrl (list, -1);
  197.             scrollok (list, FALSE);
  198.             }
  199.             scroll--;
  200.             print_item (list, items[scroll * 3],
  201.                 items[scroll * 3 + 1],
  202.                 status[scroll], 0, TRUE);
  203.             wnoutrefresh (list);
  204.  
  205.             /* print the up/down arrows */
  206.             wmove (dialog, box_y, box_x + check_x + 5);
  207.             wattrset (dialog, scroll ? uarrow_attr : menubox_attr);
  208.             waddch (dialog, scroll ? ACS_UARROW : ACS_HLINE);
  209.             wmove (dialog, box_y, box_x + check_x + 6);
  210.             waddch (dialog, scroll ? '(' : ACS_HLINE);
  211.             wmove (dialog, box_y, box_x + check_x + 7);
  212.             waddch (dialog, scroll ? '-' : ACS_HLINE);
  213.             wmove (dialog, box_y, box_x + check_x + 8);
  214.             waddch (dialog, scroll ? ')' : ACS_HLINE);
  215.             wattrset (dialog, darrow_attr);
  216.             wmove (dialog, box_y + list_height + 1,
  217.                box_x + check_x + 5);
  218.             waddch (dialog, ACS_DARROW);
  219.             wmove (dialog, box_y + list_height + 1,
  220.                box_x + check_x + 6);
  221.             waddch (dialog, '(');
  222.             wmove (dialog, box_y + list_height + 1,
  223.                box_x + check_x + 7);
  224.             waddch (dialog, '+');
  225.             wmove (dialog, box_y + list_height + 1,
  226.                box_x + check_x + 8);
  227.             waddch (dialog, ')');
  228.             wmove (dialog, cur_y, cur_x);
  229.             wrefresh (dialog);
  230.             continue;    /* wait for another key press */
  231.         } else
  232.             i = choice - 1;
  233.         } else if (key == KEY_DOWN || key == '+') {
  234.         if (choice == max_choice - 1) {
  235.             if (scroll + choice >= item_no - 1)
  236.             continue;
  237.             /* Scroll list up */
  238.             getyx (dialog, cur_y, cur_x);
  239.             if (list_height > 1) {
  240.             /* De-highlight current last item before scrolling up */
  241.             print_item (list, items[(scroll + max_choice - 1) * 3],
  242.                     items[(scroll + max_choice - 1) * 3 + 1],
  243.                     status[scroll + max_choice - 1],
  244.                     max_choice - 1, FALSE);
  245.             scrollok (list, TRUE);
  246.             scroll (list);
  247.             scrollok (list, FALSE);
  248.             }
  249.             scroll++;
  250.             print_item (list, items[(scroll + max_choice - 1) * 3],
  251.                 items[(scroll + max_choice - 1) * 3 + 1],
  252.                 status[scroll + max_choice - 1],
  253.                 max_choice - 1, TRUE);
  254.             wnoutrefresh (list);
  255.  
  256.             /* print the up/down arrows */
  257.             wattrset (dialog, uarrow_attr);
  258.             wmove (dialog, box_y, box_x + check_x + 5);
  259.             waddch (dialog, ACS_UARROW);
  260.             wmove (dialog, box_y, box_x + check_x + 6);
  261.             waddstr (dialog, "(-)");
  262.             wmove (dialog, box_y + list_height + 1,
  263.                box_x + check_x + 5);
  264.             wattrset (dialog, scroll + choice < item_no - 1 ?
  265.                   darrow_attr : menubox_border_attr);
  266.             waddch (dialog, scroll + choice < item_no - 1 ?
  267.                 ACS_DARROW : ACS_HLINE);
  268.             wmove (dialog, box_y + list_height + 1,
  269.                box_x + check_x + 6);
  270.             waddch (dialog, scroll + choice < item_no - 1 ?
  271.                 '(' : ACS_HLINE);
  272.             wmove (dialog, box_y + list_height + 1,
  273.                box_x + check_x + 7);
  274.             waddch (dialog, scroll + choice < item_no - 1 ?
  275.                 '+' : ACS_HLINE);
  276.             wmove (dialog, box_y + list_height + 1,
  277.                box_x + check_x + 8);
  278.             waddch (dialog, scroll + choice < item_no - 1 ?
  279.                 ')' : ACS_HLINE);
  280.             wmove (dialog, cur_y, cur_x);
  281.             wrefresh (dialog);
  282.             continue;    /* wait for another key press */
  283.         } else
  284.             i = choice + 1;
  285.         } else if (key == ' ') {    /* Toggle item status */
  286.         if (flag == FLAG_CHECK) {
  287.             status[scroll + choice] = !status[scroll + choice];
  288.             getyx (dialog, cur_y, cur_x);
  289.             wmove (list, choice, check_x);
  290.             wattrset (list, check_selected_attr);
  291.             wprintw (list, "[%c]", status[scroll + choice] ? 'X' : ' ');
  292.         } else {
  293.             if (!status[scroll + choice]) {
  294.             for (i = 0; i < item_no; i++)
  295.                 status[i] = 0;
  296.             status[scroll + choice] = 1;
  297.             getyx (dialog, cur_y, cur_x);
  298.             for (i = 0; i < max_choice; i++)
  299.                 print_item (list, items[(scroll + i) * 3],
  300.                     items[(scroll + i) * 3 + 1],
  301.                     status[scroll + i], i, i == choice);
  302.             }
  303.         }
  304.         wnoutrefresh (list);
  305.         wmove (dialog, cur_y, cur_x);
  306.         wrefresh (dialog);
  307.         continue;    /* wait for another key press */
  308.         }
  309.         if (i != choice) {
  310.         /* De-highlight current item */
  311.         getyx (dialog, cur_y, cur_x);
  312.         print_item (list, items[(scroll + choice) * 3],
  313.                 items[(scroll + choice) * 3 + 1],
  314.                 status[scroll + choice], choice, FALSE);
  315.         /* Highlight new item */
  316.         choice = i;
  317.         print_item (list, items[(scroll + choice) * 3],
  318.                 items[(scroll + choice) * 3 + 1],
  319.                 status[scroll + choice], choice, TRUE);
  320.         wnoutrefresh (list);
  321.         wmove (dialog, cur_y, cur_x);
  322.         wrefresh (dialog);
  323.         }
  324.         continue;        /* wait for another key press */
  325.     }
  326.     switch (key) {
  327.     case 'O':
  328.     case 'o':
  329.     case M_EVENT + 'O':
  330.         delwin (dialog);
  331.         for (i = 0; i < item_no; i++)
  332.         if (status[i]) {
  333.             if (flag == FLAG_CHECK) {
  334.             if (separate_output) {
  335.                 fprintf (stderr, "%s\n", items[i * 3]);
  336.             } else {
  337.                 fprintf (stderr, "\"%s\" ", items[i * 3]);
  338.             }
  339.             } else {
  340.             fprintf (stderr, "%s", items[i * 3]);
  341.             }
  342.  
  343.         }
  344.         free (status);
  345.         return 0;
  346.     case 'C':
  347.     case 'c':
  348.     case M_EVENT + 'C':
  349.         delwin (dialog);
  350.         free (status);
  351.         return 1;
  352.         return 1;
  353.     case M_EVENT + 'o':    /* mouse enter... */
  354.     case M_EVENT + 'c':    /* use the code for toggling */
  355.         button = (key == M_EVENT + 'o');
  356.     case TAB:
  357.     case KEY_LEFT:
  358.     case KEY_RIGHT:
  359.         if (!button) {
  360.         button = 1;    /* "Cancel" button selected */
  361.         print_button (dialog, "  OK  ", y, x, FALSE);
  362.         print_button (dialog, "Cancel", y, x + 14, TRUE);
  363.         } else {
  364.         button = 0;    /* "OK" button selected */
  365.         print_button (dialog, "Cancel", y, x + 14, FALSE);
  366.         print_button (dialog, "  OK  ", y, x, TRUE);
  367.         }
  368.         wrefresh (dialog);
  369.         break;
  370.     case ' ':
  371.     case '\n':
  372.         delwin (dialog);
  373.         if (!button)
  374.         for (i = 0; i < item_no; i++)
  375.             if (status[i]) {
  376.             if (flag == FLAG_CHECK) {
  377.                 if (separate_output) {
  378.                 fprintf (stderr, "%s\n", items[i * 3]);
  379.                 } else {
  380.                 fprintf (stderr, "\"%s\" ", items[i * 3]);
  381.                 }
  382.             } else {
  383.                 fprintf (stderr, "%s", items[i * 3]);
  384.             }
  385.  
  386.             }
  387.         free (status);
  388.         return button;
  389.     case ESC:
  390.         break;
  391.     }
  392.     }
  393.  
  394.     delwin (dialog);
  395.     free (status);
  396.     return -1;            /* ESC pressed */
  397. }
  398.